/**
 * 关注医生
 * Created by alice on 2016/7/18.
 */
require.config({
    baseUrl:'../js',
    paths:{
        "getData":"Util/getData",
        "config":"common/config",
        "components":"common/components",
        "common":"common/common",
        "jquery":"lib/jquery.min",
        "wx":"lib/jweixin-1.0.0"

    },
    shim : {
        "components": { deps : ["jquery"] }
    }
});
require(['jquery','getData','config','common','components','wx'],function($,getData,config,common,components,wx){
    var gm=common.gm;
    //获取url中的doctorUuid
    var device=gm.para.get(window.location.href,"device");
    var doctorid=gm.para.get(window.location.href,"doctorId");
    var token=gm.para.get(window.location.href,"token");
    if(!gm.common.isObjUsable(doctorid)){//判断是否存在医生id
        return;
    }
    if(device!=undefined && device=="client"){
        if(gm.isIhpone){
            console.log("ios");
        }else{
            //window.location.href="http://a.app.qq.com/o/simple.jsp?pkgname=com.hxqydyl.androidapp";
            console.log("android");
        }
    }else{ //非用户打开
        if(gm.isIhpone){
            if(gm.common.isWeiXin()){
                console.log("微信打开");
                $("#disp_wx").show();
                gm.pul.toUrl("http://a.app.qq.com/o/simple.jsp?pkgname=com.hxqydyl.androidapp");
            }else{
                gm.pul.toUrl("http://a.app.qq.com/o/simple.jsp?pkgname=com.hxqydyl.androidapp");
            }
        }else{
            window.location.href="http://a.app.qq.com/o/simple.jsp?pkgname=com.hxqydyl.androidapp";
        }
    }
    /**
     * 获取关注医生信息
     */
    gm.patient.getDoctorInfo(doctorid,function(data){
        if(!data){
            components.alert("服务器出错");
            return;
        }
        //擅长
        var territory=data.territory;
        if(!gm.common.isObjUsable(territory))
            territory="信息待完善！";
        $("#sc").html(territory);
        //医生的职称
        $("#zc").html(data.professional);
        //医生的个人介绍
        var intro=data.intro;
        if(!gm.common.isObjUsable(intro))
            intro="信息待完善！";
        $("#jj").html(intro);
        //imgUrl
        var doctorimg=gm.common.isObjUsable(data.headPath);
        $("#icon").attr("src",doctorimg?window._interfaceImgPath+""+data.headPath:gm.common.sexGetDoctorDefImg(data.sex));
        //医生姓名
        $("#nick").html(data.doctorName);
        //医生所在医院
        $("#yy").html(data.hospitalName);
        //医生所在科室
        $("#ks").html(data.deptName);
        //医生的认证状态0:未认证 1：认证通过 2：认证不通过
        $(".z_tai").attr("class","z_tai" + (data.status|| 0));

        var attentionStatus=0;
        if(data.attention==true)
            attentionStatus=1;
        //获取关注状态 1代表已关注，0代表未关注
        $(".dynamic").attr("class","dynamic" + attentionStatus);
        //关注该医生的粉丝数
        $("#fsl").html(data.clinicalReception);
        //咨询量的数量
        $("#zxl").html(data.consult);
        //接诊的数量
        $("#jzl").html(0);
        if(gm.common.isObjUsable(data.doctorId))
            $(".dalastsof").attr("value",data.doctorId);
        //初始化添加关注事件
        detailsofthedoctor(data.doctorId);

    });

    // 添加关注
    function detailsofthedoctor(objid){
        //判断用户是否关注医生
        var flag = $("#hasconcern").hasClass("dynamic0");//如果为true表示 没关注
        var num = (flag ? 1 : 0 );   //未关注为1 关注0
        var action = gm.para.get(window.location.href,"action");
        if(action=="gz"){
            if(!num){//已关注
                components.alert({
                    "val" : "已关注",
                    "type" : "flash",
                    "timer":300
                });
            }else{//未关注
                gm.common.attention(token,1,2,objid,1,function(data){
                    $("#hasconcern").attr("class","dynamic1");
                    $(".start a").html("取消关注");
                    $(".start a").css({"background-color":"#fff","color":"#a7a7a7","border":"1px solid #a7a7a7"});
                    components.alert({
                        "val" : "关注成功",
                        "type" : "flash",
                        "timer":300
                    });
                });
                /*gm.home.detailsofthe(cid,did,flag,function(data){//直接关注
                    $("#hasconcern").attr("class","dynamic1");
                    $(".start a").html("取消关注");
                    $(".start a").css({"background-color":"#fff","color":"#a7a7a7","border":"1px solid #a7a7a7"});
                    components.alert({
                        "val" : "关注成功",
                        "type" : "flash",
                        "timer":300
                    });
                });*/
            }

        }

        if(!num){//如果已经关注
            $(".start a").html("取消关注");
            $(".start a").css({"background-color":"#fff","color":"#a7a7a7","border":"1px solid #a7a7a7"});
        }

        // 关注医生 bind 关注
        $(".start,.dynamic1,.dynamic0").bind({
            "touchend":function(e){
                e.preventDefault();
                var flag = $("#hasconcern").hasClass("dynamic0");//如果为true表示 没关注
                var attentionState=flag?1:2;//未关注为1 已关注为2
                num = (flag ? 1 : 0 );//未关注为1 关注0
                var getid=$(".dalastsof").attr("value");//医生的id
                gm.common.attention(token,1,2,getid,attentionState,function(data){
                    $(".dynamic0,.dynamic1").attr("class","dynamic" + num);
                    if(num){
                        components.alert({
                            "val" : "关注成功",
                            "type" : "flash"
                        });
                        $(".start a").html("取消关注");
                        $(".start a").css({"background-color":"#fff","color":"#a7a7a7","border":"1px solid #a7a7a7"});
                    }else{
                        components.alert({
                            "val" : "取消关注成功",
                            "type" : "flash"
                        });
                        $(".start a").html("关注");
                        $(".start a").css({"background":"#28c3b1","color":"#fff","border":"1px solid #28c3b1"});
                    }
                });

            }
        });
    }
});